import os
import cv2
import datetime

def get_file_last_modified_time(file_path):
    # 获取文件最后修改时间
    modification_time = os.path.getmtime(file_path)
    return modification_time

def compare_file_modification(file1, file2):
    time1 = get_file_last_modified_time(file1)
    time2 = get_file_last_modified_time(file2)

    if time1 > time2:
        return file1
    elif time2 > time1:
        return file2
    else:
        return "Both files have the same modification time."

def get_file_last_modified_time(file_path):
    # 获取文件最后修改时间
    modification_time = os.path.getmtime(file_path)

    # 将时间戳转换为可读的日期时间格式
    modified_time = datetime.datetime.fromtimestamp(modification_time)

    return modified_time

# 读取YOLO格式的标注文件
def read_annotation_file(annotation_file):
    with open(annotation_file, 'r') as file:
        annotation = file.readline().strip()
    return annotation

# 解析标注内容
def parse_annotation(annotation):
    data = annotation.split()
    category = int(data[0])
    points = [float(val) for val in data[1:]]
    return category, points

def show_one(annotation_file, image_path, window_name):
    # 读取标注文件
    annotation = read_annotation_file(annotation_file)
    print(f"{window_name} {annotation}")
    print(get_file_last_modified_time(annotation_file))

    # 解析标注内容
    category, points = parse_annotation(annotation)

    # 读取图像文件（假设图像文件与标注文件名称一致，且位于文件夹B）
    image = cv2.imread(image_path)

    # 获取图像的宽度和高度
    height, width, _ = image.shape

    # 计算四个点的实际像素坐标
    x1 = int(points[0] * width)
    y1 = int(points[1] * height)
    x2 = int(points[2] * width)
    y2 = int(points[3] * height)
    x3 = int(points[4] * width)
    y3 = int(points[5] * height)
    x4 = int(points[6] * width)
    y4 = int(points[7] * height)

    # 在图像上绘制四个点
    cv2.circle(image, (x1, y1), 3, (255, 0, 0), 1)  # 红色
    cv2.circle(image, (x2, y2), 3, (0, 255, 0), 1)  # 绿色
    cv2.circle(image, (x3, y3), 3, (0, 0, 255), 1)  # 蓝色
    cv2.circle(image, (x4, y4), 3, (255, 255, 0), 1)  # 青色

    # 显示图像
    cv2.namedWindow(window_name, 0)
    cv2.imshow(window_name, image)



show_one("/home/champrin/Desktop/MV-CS016-10UC+DA1041860/blue/Video_20250113103836696/yolo_annotations/exchange_blue_Video_20250113103836696_71.txt",
         "/home/champrin/Desktop/MV-CS016-10UC+DA1041860/blue/Video_20250113103836696/yolo_annotations/exchange_blue_Video_20250113103836696_71.jpg",
         "0")

# show_one("/home/champrin/Downloads/23年数据集/分区赛/rock/remove/home/label/H-BLUE-169.txt",
#          "/home/champrin/Downloads/23年数据集/分区赛/rock/remove/home/images/H-BLUE-169.jpg",
#          "1")

cv2.waitKey(0)
cv2.destroyAllWindows()